from interface.widget import Page, scale, YScrollableContainer
from tkinter import ttk
import tkinter
from utils.network import request, send_file
from utils import memory
from tkinter import messagebox, filedialog
import pathlib


class MultiSendFilePage(Page):

    def __init__(self, master):
        Page.__init__(self, master)
        self.account2is_select = {}
        self.file_path = None

    def content(self, master):
        back_button = ttk.Button(master, text='返回', command=self.master.close_page)
        back_button.place(**scale(x=40, y=30, width=80, height=30))

        ttk.Style().configure('MultiSendFilePage.TitleLabel.TLabel', anchor='center', font=('', 14))
        title_label = ttk.Label(master, text='群发文件', style='MultiSendFilePage.TitleLabel.TLabel')
        title_label.place(**scale(relx=0.5, x=-80, y=30, width=160, height=30))

        right_container = ttk.Frame(master)
        right_container.place(**scale(relx=0.4, y=80, relwidth=0.6, width=-40, relheight=1, height=-100))

        as_image_var = tkinter.BooleanVar()
        as_image_button = ttk.Checkbutton(right_container, text='作为图片发送', variable=as_image_var)
        as_image_button.place(**scale(x=100, y=0, width=150, height=30))

        file_name_label = ttk.Label(right_container, text='点击按钮选择您要发送的文件')
        file_name_label.place(**scale(x=0, y=50))

        progressbar = ttk.Progressbar(right_container, value=0, maximum=100)
        progressbar.place(**scale(x=0, y=100, relwidth=1, height=20))

        def file_select_button_method():
            file_path = pathlib.Path(filedialog.askopenfilename())
            self.file_path = file_path
            file_name_label['text'] = file_path

        file_select_button = ttk.Button(right_container, text='选择文件', command=file_select_button_method)
        file_select_button.place(**scale(x=0, y=0, width=80, height=30))

        def send_button_method():
            recipients = []
            for account in self.account2is_select.keys():
                if self.account2is_select[account].get():
                    recipients.append(account)

            def hook(rate):
                progressbar['value'] = rate * 100
                progressbar.update()

            send_file(self.file_path, self.file_path.name, recipients, hook, as_image_var.get())

        send_button = ttk.Button(master, text='发送', command=send_button_method)
        send_button.place(**scale(relx=1, x=-120, y=30, width=80, height=30))

        user_select_container = YScrollableContainer(master)
        user_select_container.x = 40
        user_select_container.y = 80
        user_select_container.relwidth = 0.4
        user_select_container.width = -80
        user_select_container.relheight = 1
        user_select_container.height = -100
        user_select_container.display()

        for user in memory.users.values():
            var = tkinter.BooleanVar()
            check_button = ttk.Checkbutton(user_select_container.inner.tkw, variable=var, text='{} ({})'.format(
                user['username'], user['account']
            ))
            check_button.place(**scale(x=0, y=30 * len(self.account2is_select), relwidth=1, height=30))
            self.account2is_select[user['account']] = var
            user_select_container.update()
